package com.zlt.bkparking.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zlt.bkparking.dto.AppUserDTO;
import com.zlt.bkparking.dto.UserVipDTO;
import com.zlt.bkparking.dto.VipTypeDTO;
import com.zlt.bkparking.entity.*;
import com.zlt.bkparking.mapper.AppUserMapper;
import com.zlt.bkparking.mapper.UserVipMapper;
import com.zlt.bkparking.mapper.VipTypeMapper;
import com.zlt.bkparking.service.VIPTypeService;
import com.zlt.bkparking.utils.StringUtil;
import com.zlt.bkparking.utils.TimeUtil;
import com.zlt.bkparking.vo.AppUserVO;
import com.zlt.bkparking.vo.UserVipVO;
import com.zlt.bkparking.vo.VipTypeVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname VIPTypeServiceImpl
 * @Description TODO
 * @Date 2022/10/3 8:54
 * @Created by ${Lijijun}
 */
@Service
public class VIPTypeServiceImpl implements VIPTypeService {

    @Autowired
    private VipTypeMapper vipTypeMapper;

    @Autowired
    private UserVipMapper userVipMapper;

    @Autowired
    private AppUserMapper appUserMapper;

    @Override
    public List<VipTypeVO> selectVipTypes() {
        VipTypeExample vipTypeExample = new VipTypeExample();
        List<VipType> vipTypes = vipTypeMapper.selectByExample(vipTypeExample);
        List<VipTypeVO> vipTypeVOList = new ArrayList<>();
        if (vipTypes!=null && !vipTypes.isEmpty()){
            for (VipType vipType : vipTypes) {
                VipTypeVO vipTypeVO = new VipTypeVO();
                vipTypeVOList.add(vipTypeVO);
                BeanUtils.copyProperties(vipType,vipTypeVO);
            }
        }
        return vipTypeVOList;
    }

    @Override
    public boolean updateVipType(VipTypeDTO vipTypeDTO) {
        VipType vipType = new VipType();
        BeanUtils.copyProperties(vipTypeDTO,vipType);
        if (vipType.getVtId()==null)
            return false;
        int i = vipTypeMapper.updateByPrimaryKeySelective(vipType);
        return i>0;
    }

    @Override
    public boolean insertVipType(VipTypeDTO vipTypeDTO) {
        VipType vipType = new VipType();
        BeanUtils.copyProperties(vipTypeDTO,vipType);
        if (StringUtil.isBlank(vipType.getVtName())){
            throw new RuntimeException("Vip类型名不能为空");
        }
        if (vipType.getVtPrice()==null){
            throw new RuntimeException("Vip价格不能为空");
        }
        if (vipType.getVtDay()==null){
            throw new RuntimeException("Vip单价对应天数不能为空");
        }
        int i = vipTypeMapper.insertSelective(vipType);
        return i>0;
    }

    @Override
    public boolean deleteVipType(VipTypeDTO vipTypeDTO) {
        if (vipTypeDTO.getVtId()==null)
            return false;
        int i = vipTypeMapper.deleteByPrimaryKey(vipTypeDTO.getVtId());
        return i>0;
    }

    @Override
    public PageInfo<UserVipVO> selectVipUsers(UserVipDTO userVipDTO) {
        Integer page = userVipDTO.getPage();
        if (page==null){
            page =1;
        }
        Integer limit = userVipDTO.getLimit();
        if (limit==null){
            limit=5;
        }
        AppUserExample appUserExample = new AppUserExample();
        Boolean flag1=false;
        Boolean flag2=false;
        if (StringUtil.isNotNull(userVipDTO.getAuUsername())){
            appUserExample.createCriteria().andAuUsernameLike("%"+userVipDTO.getAuUsername()+"%");
        }
        List<AppUser> appUserList=null;
        if (StringUtil.isNotNull(userVipDTO.getAuUsername())){
            appUserList=appUserMapper.selectByExample(appUserExample);
            flag1=true;
        }
        VipTypeExample vipTypeExample = new VipTypeExample();
        if (StringUtil.isNotNull(userVipDTO.getVtName())){
            vipTypeExample.createCriteria().andVtNameLike("%"+userVipDTO.getVtName()+"%");
        }
        List<VipType> vipTypeList=null;
        if (StringUtil.isNotNull(userVipDTO.getVtName())){
            vipTypeList=vipTypeMapper.selectByExample(vipTypeExample);
            flag2=true;
        }
        UserVipExample userVipExample = new UserVipExample();
        List<String> appUserIds=new ArrayList<>();
        appUserIds.add("");
        if (appUserList!=null && !appUserList.isEmpty() ||flag1){//flag 代表查询过  是条件查询
            if (appUserList!=null && !appUserList.isEmpty()){
                for (AppUser appUser : appUserList) {
                    appUserIds.add(appUser.getAuId());
                }
            }
            userVipExample.createCriteria().andAuIdIn(appUserIds);
        }
        List<Integer> vipTypeIds=new ArrayList<>();
        vipTypeIds.add(Integer.MAX_VALUE);
        if (vipTypeList!=null&& !vipTypeList.isEmpty() ||flag2){
            if (vipTypeList!=null&& !vipTypeList.isEmpty()){
                for (VipType vipType : vipTypeList) {
                    vipTypeIds.add(vipType.getVtId());
                }
            }
            userVipExample.createCriteria().andVtIdIn(vipTypeIds);
        }
        PageHelper.startPage(page,limit);
        List<UserVip> userVips = userVipMapper.selectByExample(userVipExample);
        PageInfo<UserVipVO> userVipVOPageInfo = new PageInfo(userVips);
        List<UserVipVO> userVipVOList = new ArrayList<>();
        userVipVOPageInfo.setList(userVipVOList);
        if (userVips!=null && !userVips.isEmpty()){
            for (UserVip userVip : userVips) {
                UserVipVO userVipVO = new UserVipVO();
                userVipVOList.add(userVipVO);
                BeanUtils.copyProperties(userVip,userVipVO);
                if (StringUtil.isNotNull(userVipVO.getAuId())){
                    AppUser appUser = appUserMapper.selectByPrimaryKey(userVipVO.getAuId());
                    if (appUser!=null){
                        AppUserVO appUserVO = new AppUserVO();
                        BeanUtils.copyProperties(appUser,appUserVO);
                        userVipVO.setAppUserVO(appUserVO);
                    }

                }
                if (userVipVO.getVtId()!=null){
                    VipType vipType = vipTypeMapper.selectByPrimaryKey(userVipVO.getVtId());
                    if (vipType!=null){
                        VipTypeVO vipTypeVO = new VipTypeVO();
                        BeanUtils.copyProperties(vipType,vipTypeVO);
                        userVipVO.setVipTypeVO(vipTypeVO);
                    }
                }
            }
        }


        return userVipVOPageInfo;
    }

    @Override
    public boolean updateVipUser(UserVipDTO userVipDTO) {
        UserVip userVip = new UserVip();
        BeanUtils.copyProperties(userVipDTO,userVip);
        if (userVip.getUvId()==null){
            throw  new RuntimeException("Vip用户的信息修改失败--主键不能为空");
        }
        int i = userVipMapper.updateByPrimaryKeySelective(userVip);
        return i>0;
    }

    @Override
    public boolean updateVipUserState(UserVipDTO userVipDTO) {
        UserVip userVip = new UserVip();
        BeanUtils.copyProperties(userVipDTO,userVip);
        if (userVip.getUvId()==null)
            throw new RuntimeException("主键不能为空");
        if (userVip.getUvState()==null)
            throw new RuntimeException("vip状态不能为空");
        Byte uvState = userVip.getUvState();
        UserVip originalUserVip = userVipMapper.selectByPrimaryKey(userVipDTO.getUvId());
        if (originalUserVip==null)
            throw new RuntimeException("没有该用户的vip记录");
        Boolean bool1 =false;
        Boolean bool0 =false;
        if (uvState.equals((byte)1)){
            bool1=true;
            if (originalUserVip.getUvState().equals(1)){
                throw new RuntimeException("该vip用户已经是启用状态了");
            }
        }
        if (uvState.equals((byte)0)){
            bool0=true;
            if (originalUserVip.getUvState().equals(0)){
                throw new RuntimeException("该vip用户已经是禁用状态了");
            }
        }
        if (bool1){
//            userVip.setUvEnd(new Date());
            int i = userVipMapper.updateByPrimaryKeySelective(userVip);
            return i>0;
        }
        if (bool0){
//            userVip.setUvEnd(TimeUtil.incrementDate());
            int i = userVipMapper.updateByPrimaryKeySelective(userVip);
            return i>0;
        }
        return false;
    }
}
